//
//  NewHomeButtonTableViewCell.m
//  GC_OC
//
//  Created by  Quan He on 2022/4/9.
//
#import "HomeButtonModel.h"
#import "NewHomeButtonTableViewCell.h"
#import "NewHomeViewController.h"
#import "AudioViewController.h"

@interface NewHomeButtonTableViewCell()
@property (nonatomic, strong) UIView *buttonBackView;
@property (nonatomic, strong) UIButton *editButton;

@end

@implementation NewHomeButtonTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self initUI];
        _buttonArr = [NSMutableArray array];
        self.contentView.clipsToBounds = YES;
        [self.contentView addSubview:self.buttonBackView];
        [self.buttonBackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@(0));
            make.top.equalTo(@(12));
            make.bottom.equalTo(@(-8));
        }];
        [self.buttonBackView addSubview:self.editButton];
    }
    return self;
}

- (void)initUI {
    self.contentView.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.00];
    self.backgroundColor = [UIColor colorWithRed:0.96 green:0.97 blue:0.98 alpha:1.00];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
}

- (void)fillTheData:(NSMutableArray *)array{
    [_buttonArr removeAllObjects];
    [_buttonArr addObjectsFromArray:array];
    CGFloat space = 8;
    CGFloat height = 36;
    CGFloat width = (SCREEN_WIDTH - space * 3 - 32) * 0.25;
    for (int index = 0; index < array.count; index ++) {
        HomeButtonModel *model = array[index];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.backgroundColor = [UIColor whiteColor];
        button.frame = CGRectMake(16 + (width+space)*(index%4), (36 + 12) * (index / 4), width, height);
        button.layer.cornerRadius = height * 0.5;
        button.layer.masksToBounds = YES;
        button.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightLight];
        [button setTitleColor:[UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:1.00] forState:UIControlStateNormal];
        button.adjustsImageWhenHighlighted = NO;
        button.titleLabel.adjustsFontSizeToFitWidth = YES;
        [button addTarget:self action:@selector(chooseAction:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 700+index;
        [button setTitle:model.name forState:UIControlStateNormal];
        button.contentEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 10);
        [self.buttonBackView addSubview:button];
    }
    self.editButton.hidden = array.count <= 8;
    [self.buttonBackView bringSubviewToFront:self.editButton];
}

- (void)hideButtonAction {
    self.hide = !_hide;
    self.hideAction(_hide);
}

- (void)setHide:(BOOL)hide {
    
    _hide = hide;
    CGFloat space = 8;
    CGFloat height = 36;
    CGFloat width = (SCREEN_WIDTH - space * 3 - 32) * 0.25;
    NSInteger index = 7;
    NSString *title = @"更多";
    NSString *imageName = @"small_xiangxia";
    if (!_hide) {
        title = @"收起";
        imageName = @"small_xiangshang";
        index = self.buttonArr.count;
    }
    [self.editButton setTitle:title forState:UIControlStateNormal];
    [self.editButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    self.editButton.frame = CGRectMake(16 + (width+space)*(index%4), (36 + 12) * (index / 4), width, height);
    self.editButton.hidden = self.buttonArr.count <= 8;
}

- (void)chooseAction:(UIButton *)button {
    HomeButtonModel *model = self.buttonArr[button.tag - 700];
    if ([UIViewController currentViewController].navigationController.viewControllers.count > 1) {
        AudioViewController* homeOne=[[AudioViewController alloc]init];
        homeOne.hidesBottomBarWhenPushed = YES;
        homeOne.idString = self.classId;
        homeOne.title = model.name;
        homeOne.tagString = model.id;
        homeOne.imageUrl = model.media_image;
        homeOne.isMenu = YES;
        homeOne.describe = model.describe;
        UIViewController *controler = [UIViewController currentViewController];
        if ([controler.class isKindOfClass:NSClassFromString(@"NewHomeViewController")]) {
            homeOne.parentName = ((NewHomeViewController *)controler).titleString;
        }else {
            homeOne.parentName = controler.title;
        }
        [controler.navigationController pushViewController:homeOne animated:YES];
    }else {
        NewHomeViewController* homeVC = [[NewHomeViewController alloc]initWithId:model.id isHomePage:NO];
        homeVC.title = model.name;
        [[UIViewController currentViewController].navigationController pushViewController:homeVC animated:YES];
    }
}

- (UIButton *)editButton {
    if (!_editButton) {
        _editButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.backgroundColor = [UIColor whiteColor];
            button.layer.cornerRadius = 18;
            button.layer.masksToBounds = YES;
            button.titleLabel.font = [UIFont systemFontOfSize:14 weight:UIFontWeightLight];
            [button setTitleColor:[UIColor colorWithRed:0.04 green:0.04 blue:0.04 alpha:1.00] forState:UIControlStateNormal];
            button.adjustsImageWhenHighlighted = NO;
            [button addTarget:self action:@selector(hideButtonAction) forControlEvents:UIControlEventTouchUpInside];
            [button setTitle:@"更多" forState:UIControlStateNormal];
            [button setImage:[UIImage imageNamed:@"small_xiangxia"] forState:UIControlStateNormal];
            [button setImagePosition:SYImagePositionRight spacing:4];
            button.hidden = YES;
            button;
        });
    }
    return _editButton;
}

- (UIView *)buttonBackView {
    if (!_buttonBackView) {
        _buttonBackView = [[UIView alloc] init];
        _buttonBackView.clipsToBounds = YES;
    }
    return _buttonBackView;
}

@end
